package aop.ano;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author wang fei
 * @date 2020-10-11 15:16
 * 2.创建增强的类
 * 增强的类
 */
@Component
@Aspect //生成代理对象
@Order(3)
public class UserProxy {

    /**
     * 相同切入点抽取
     */
    @Pointcut(value = "execution(* aop.ano.User.add(..))")
    public void pointDemo(){

    }

    //前置增强
    @Before(value = "pointDemo()")
    public void before(){
        System.out.println("before............");
    }

    //最终通知【有异常也执行】
    @After(value = "execution(* aop.ano.User.add(..))")
    public void after(){
        System.out.println("after..........");
    }

    //后置通知（返回通知）【有异常不执行】
    @AfterReturning(value = "execution(* aop.ano.User.add(..))")
    public void afterReturning(){
        System.out.println("afterReturning..........");
    }

    //有异常执行
    @AfterThrowing(value = "execution(* aop.ano.User.add(..))")
    public void afterThrowing(){
        System.out.println("afterThrowing..........");
    }

    //环绕通知【方法前后】方法报错后置不执行
    @Around(value = "execution(* aop.ano.User.add(..))")
    public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕之前..........");

        //被增强的方法
        proceedingJoinPoint.proceed();

        System.out.println("环绕之后..........");
    }
}
